#include "gofgame/dungeonkeeper/ui/UnitElementView.h"
#include "gofgame/dungeonkeeper/ui/UnitUpgradeEventArgs.h"
#include "gofgame/dungeonkeeper/ui/MessageViewDialog.h"
#include "gofgame/common/ui/TableLayout.h"
#include "gofgame/common/ui/TableLayoutData.h"
#include "gofgame/common/ui/TableColumn.h"
#include "gofgame/common/graphics/HornedTextureBrush.h"
#include "gofgame/common/graphics/LinearGradientBrush.h"

using namespace gofgame::dungeonkeeper::ui;
using namespace gofgame::common::graphics;

HornedTextureBrushResource UnitElementView::elementBrushResource(20, 20, "gofgame/dungeonkeeper/ui/element.png");
TextureResource UnitElementView::hpIconResource("gofgame/dungeonkeeper/ui/hp_icon.png");
TextureResource UnitElementView::attackIconResource("gofgame/dungeonkeeper/ui/attack_icon.png");
TextureResource UnitElementView::defIconResource("gofgame/dungeonkeeper/ui/def_icon.png");
TextureResource UnitElementView::priceIconResource("gofgame/dungeonkeeper/ui/price_icon.png");

bool UnitElementView::init() {
	if (!Container::init()) {
		return false;
	}

	this->setBackgroundBrush(elementBrushResource.unwrap());
	this->setPadding(20);
	TableLayout *tableLayout = new TableLayout(30);
	this->setHeight(
		tableLayout->getMinRowHeight() * 3 + 
		this->getPaddingTop() + 
		this->getPaddingBottom());

	tableLayout->setCellPadding(2);
	TableLayoutData *tableLayoutData;
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::absolute(150));
	this->setLayout(tableLayout);

	this->image.initialize();
	this->image->init();
	this->image->setResourceName("Base/aibase.png");
	tableLayoutData = new TableLayoutData();
	tableLayoutData->setRowSpan(3);
	this->image->setLayoutData(tableLayoutData);
	this->addChild(this->image);

	Ref<Label> nameDescLabel;
	nameDescLabel.initialize();
	nameDescLabel->init();
	nameDescLabel->setText("Name: ");
	nameDescLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(nameDescLabel);
	this->nameLabel.initialize();
	this->nameLabel->init();
	this->nameLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->nameLabel);

	Ref<Image> hpDescImage;
	hpDescImage.initialize();
	hpDescImage->init();
	hpDescImage->setTexture(hpIconResource.unwrap());
	hpDescImage->setDisplayMode(ImageDisplayMode_Right);
	this->addChild(hpDescImage);
	this->hpLabel.initialize();
	this->hpLabel->init();
	this->hpLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->hpLabel);

	Ref<Label> menaDescLabel;
	menaDescLabel.initialize();
	menaDescLabel->init();
	menaDescLabel->setText("Mena: ");
	menaDescLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(menaDescLabel);
	this->menaLabel.initialize();
	this->menaLabel->init();
	this->menaLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->menaLabel);

	Ref<Image> attackDescImage;
	attackDescImage.initialize();
	attackDescImage->init();
	attackDescImage->setTexture(attackIconResource.unwrap());
	attackDescImage->setDisplayMode(ImageDisplayMode_Right);
	this->addChild(attackDescImage);
	this->attackLabel.initialize();
	this->attackLabel->init();
	this->attackLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->attackLabel);

	this->upgradeButton.initialize();
	this->upgradeButton->init();
	tableLayoutData = new TableLayoutData();
	tableLayoutData->setRowSpan(3);
	this->upgradeButton->setLayoutData(tableLayoutData);
	this->upgradeButton->setBackgroundBrush(
		new HornedTextureBrush(
			CCSize(10, 10),
			CCTextureCache::sharedTextureCache()->addImage(
				"gofgame/dungeonkeeper/ui/upgrade_button.png"
			),
			Brush::white()
		)
	);
	this->upgradeButton->setPressedBackgroundBrush(
		new HornedTextureBrush(
			CCSize(10, 10),
			CCTextureCache::sharedTextureCache()->addImage(
				"gofgame/dungeonkeeper/ui/upgrade_button.png"
			),
			new LinearGradientBrush(0, 0, 0, 0, 1, 0, 0, 1)
		)
	);
	this->addChild(this->upgradeButton);

	Ref<Image> defDescImage;
	defDescImage.initialize();
	defDescImage->init();
	defDescImage->setTexture(defIconResource.unwrap());
	defDescImage->setDisplayMode(ImageDisplayMode_Right);
	this->addChild(defDescImage);
	this->defLabel.initialize();
	this->defLabel->init();
	this->defLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->defLabel);

	Ref<Label> speedDescLabel;
	speedDescLabel.initialize();
	speedDescLabel->init();
	speedDescLabel->setText("Speed: ");
	speedDescLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(speedDescLabel);
	this->speedLabel.initialize();
	this->speedLabel->init();
	this->speedLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->speedLabel);

	Ref<Label> attackSpeedDescLabel;
	attackSpeedDescLabel.initialize();
	attackSpeedDescLabel->init();
	attackSpeedDescLabel->setText("AttackSpeed: ");
	attackSpeedDescLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(attackSpeedDescLabel);
	this->attackSpeedLabel.initialize();
	this->attackSpeedLabel->init();
	this->attackLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->attackSpeedLabel);

	Ref<Image> priceDescImage;
	priceDescImage.initialize();
	priceDescImage->init();
	priceDescImage->setTexture(priceIconResource.unwrap());
	priceDescImage->setDisplayMode(ImageDisplayMode_Right);
	this->addChild(priceDescImage);
	this->priceLabel.initialize();
	this->priceLabel->init();
	this->priceLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->priceLabel);

	Ref<Label> descriptionDescLabel;
	descriptionDescLabel.initialize();
	descriptionDescLabel->init();
	descriptionDescLabel->setText("Description: ");
	this->addChild(descriptionDescLabel);
	this->descriptionLabel.initialize();
	this->descriptionLabel->init();
	tableLayoutData = new TableLayoutData();
	tableLayoutData->setColSpan(8);
	this->descriptionLabel->setLayoutData(tableLayoutData);
	this->descriptionLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->descriptionLabel);

	this->upgradeButton->click() += event_handler<ComponentEventArgs>(
		this, &UnitElementView::upgradeButton_click);

	return true;
}

UnitElementViewInfo UnitElementView::getUnitElementViewInfo() const {
	UnitElementViewInfo uevi;
	uevi.hp = atoi(this->hpLabel->getText());
	uevi.mp = atoi(this->menaLabel->getText());
	uevi.attack = atoi(this->attackLabel->getText());
	uevi.def = atoi(this->defLabel->getText());
	uevi.speed = atoi(this->speedLabel->getText());
	uevi.attackSpeed = atoi(this->attackSpeedLabel->getText());
	uevi.price = atoi(this->priceLabel->getText());
	uevi.desc = atoi(this->descriptionLabel->getText());
	return uevi;
}

void UnitElementView::setUnitElementViewInfo(const UnitElementViewInfo &uevi) {
	char text[20];
	this->nameLabel->setText(uevi.name.c_str());
	sprintf(text, "%d", uevi.hp);
	this->hpLabel->setText(text);
	sprintf(text, "%d", uevi.mp);
	this->menaLabel->setText(text);
	sprintf(text, "%d", uevi.attack);
	this->attackLabel->setText(text);
	sprintf(text, "%d", uevi.def);
	this->defLabel->setText(text);
	sprintf(text, "%d", uevi.speed);
	this->speedLabel->setText(text);
	sprintf(text, "%d", uevi.attackSpeed);
	this->attackSpeedLabel->setText(text);
	sprintf(text, "%d", uevi.price);
	this->priceLabel->setText(text);
	this->descriptionLabel->setText(uevi.desc.c_str());
}

void UnitElementView::upgradeButton_click(void *sender, ComponentEventArgs &e) {
	Ref<MessageDialog> dialog;
	dialog.initialize(new MessageViewDialog(MessageDialogType_OKCancel));
	dialog->init();
	dialog->setTitle("Are you sure...");
	dialog->setMessage("Are you sure to upgrade this skill?");
	dialog->closed() += event_handler<DialogClosedEventArgs>(
		this, &UnitElementView::upgradeConfirmDialog_close);
	dialog->show();
}

void UnitElementView::upgradeConfirmDialog_close(void* sender, DialogClosedEventArgs &e) {
	if (e.getDialogResult() == DialogResult_OK) {
		UnitUpgradeEventArgs upgradeEventArgs(this, this->getUnitElementViewInfo());
		this->fireManualBubbleEvent(upgradeEventArgs);
		this->setUnitElementViewInfo(upgradeEventArgs.getUnitElementViewInfo());
	}
}